// import { createChunk } from './createChunks.js'

// const workercode = () => {
//   self.onmessage = function (e) {
//     let workerResult = `主线程发来的: ${e.data}`
//     console.log(workerResult)
//     self.postMessage('子线程返回的：zzz')
//   }
// }
// import SparkMD5 from './spark-md5.min.js'
// import SparkMD5 from 'spark-md5'
const workercode = () => {
  self.importScript('https://cdn.bootcdn.net/ajax/libs/spark-md5/3.0.2/spark-md5.js')
  function createChunk(file, index, chunkSize) {
    return new Promise((resolve) => {
      const start = index * chunkSize
      const end = start * chunkSize
      const spark = new SparkMD5.ArrayBuffer()
      const fileReader = new FileReader()
      fileReader.onload = (e) => {
        e.target?.result && spark.append(e.target?.result)
        resolve({
          start,
          end,
          index,
          hash: spark.end(),
        })
      }
      fileReader.readAsArrayBuffer(file.slice(start, end))
    })
  }
  // self.importScript('https://cdn.bootcdn.net/ajax/libs/spark-md5/3.0.2/spark-md5.js')
  self.onmessage = (e) => {
    const promise = []
    const { file, CHUNK_SIZE, startIndex, endIndex } = e.data
    console.log(file, CHUNK_SIZE, startIndex, endIndex)
    for (let i = startIndex; i < endIndex; i++) {
      promise.push(createChunk(file, i, CHUNK_SIZE))
    }
    Promise.all(promise).then((chunks) => {
      postMessage(chunks)
    })
  }
}
let code = workercode.toString()
code = code.substring(code.indexOf('{') + 1, code.lastIndexOf('}'))
const blob = new Blob([code], { type: 'application/javascript' })
const worker_script = URL.createObjectURL(blob)

export default worker_script

// onmessage = async (e) => {
//   const promise = []
//   const { file, CHUNK_SIZE, startIndex, endIndex } = e.data
//   console.log(file, CHUNK_SIZE, startIndex, endIndex)
//   for (let i = startIndex; i < endIndex; i++) {
//     promise.push(createChunk(file, i, CHUNK_SIZE))
//   }
//   const chunks = await Promise.all(promise)
//   postMessage(chunks)
// }

// worker.js
// 定义worker线程脚本代码
// const workercode = () => {
//   self.onmessage = async (e) => {
//     const promise = []
//     const { file, CHUNK_SIZE, startIndex, endIndex } = e.data
//     console.log(file, CHUNK_SIZE, startIndex, endIndex)
//     // for (let i = startIndex; i < endIndex; i++) {
//     //   promise.push(createChunk(file, i, CHUNK_SIZE))
//     // }
//     // const chunks = await Promise.all(promise)
//     postMessage('111')
//   }
// }
// // 把脚本代码转为string
// let code = workercode.toString()
// code = code.substring(code.indexOf('{') + 1, code.lastIndexOf('}'))

// const blob = new Blob([code], { type: 'application/javascript' })
// const worker_script = URL.createObjectURL(blob)

// export default worker_script

// class WebWorker {
//   constructor(worker) {
//     let code = worker.toString()
//     code = code.substring(code.indexOf('{') + 1, code.lastIndexOf('}'))

//     const blob = new Blob([code], { type: 'text/javascript' })
//     // worker地址为同源地址
//     return new Worker(URL.createObjectURL(blob))
//   }
// }

// export const workers = () => {
//   function workerCode() {
//     this.onmessage = async (e) => {
//       const promise = []
//       const { file, CHUNK_SIZE, startIndex, endIndex } = e.data
//       console.log(file, CHUNK_SIZE, startIndex, endIndex)
//       for (let i = startIndex; i < endIndex; i++) {
//         promise.push(createChunk(file, i, CHUNK_SIZE))
//       }
//       const chunks = await Promise.all(promise)
//       postMessage(chunks)
//     }
//     this.postMessage({})
//   }
//   const myWorker = new WebWorker(workerCode, { type: 'module' })
//   return myWorker
// }
